package gat.common.util;

import cn.hutool.core.date.DateUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class CalendarUtil {
	public static void main(String[] args) {
		// 获取当前年份、月份、日期
		Calendar cale = null;
		cale = Calendar.getInstance();
		int year = cale.get(Calendar.YEAR);
		int month = cale.get(Calendar.MONTH) + 1;
		int day = cale.get(Calendar.DATE);
		int hour = cale.get(Calendar.HOUR_OF_DAY);
		int minute = cale.get(Calendar.MINUTE);
		int second = cale.get(Calendar.SECOND);
		int dow = cale.get(Calendar.DAY_OF_WEEK);
		int dom = cale.get(Calendar.DAY_OF_MONTH);
		int doy = cale.get(Calendar.DAY_OF_YEAR);
 
		System.out.println("Current Date: " + cale.getTime());
		System.out.println("Year: " + year);
		System.out.println("Month: " + month);
		System.out.println("Day: " + day);
		System.out.println("Hour: " + hour);
		System.out.println("Minute: " + minute);
		System.out.println("Second: " + second);
		System.out.println("Day of Week: " + dow);
		System.out.println("Day of Month: " + dom);
		System.out.println("Day of Year: " + doy);
 
		// 获取当月第一天和最后一天
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

		String firstday, lastday;
		// 获取前月的第一天
		cale = Calendar.getInstance();
		cale.add(Calendar.MONTH, 0);
		cale.set(Calendar.DAY_OF_MONTH, 1);
		firstday = format.format(cale.getTime());
		// 获取前月的最后一天
		cale = Calendar.getInstance();
		cale.add(Calendar.MONTH, 1);
		cale.set(Calendar.DAY_OF_MONTH, 0);
		lastday = format.format(cale.getTime());
		System.out.println("本月第一天和最后一天分别是 ： " + firstday + " and " + lastday);
 
		// 获取当前日期字符串
		Date d = new Date();
		System.out.println("当前日期字符串1：" + format.format(d));
		System.out.println("当前日期字符串2：" + year + "/" + month + "/" + day + " "
				+ hour + ":" + minute + ":" + second);
	}

	 public static String getFirstDay(){
		 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		 Calendar cale = null;
		 String firstDay;
		 // 获取前月的第一天
		 cale = Calendar.getInstance();
		 cale.add(Calendar.MONTH, 0);
		 cale.set(Calendar.DAY_OF_MONTH, 1);
		 return format.format(cale.getTime());
	}
	public static String getLastDay(){
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cale = null;
		// 获取当前月的最后一天
		cale = Calendar.getInstance();
		cale.add(Calendar.MONTH, 1);
		cale.set(Calendar.DAY_OF_MONTH, 0);
		return format.format(cale.getTime());

	}

	/**
	 *  获取两个日期之间的所有日期 (年月日)
	 *
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	public static List<String> getBetweenDate(String startTime, String endTime){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		// 声明保存日期集合
		List<String> list = new ArrayList<String>();
		try {
			// 转化成日期类型
			Date startDate = sdf.parse(startTime);
			Date endDate = sdf.parse(endTime);
			//用Calendar 进行日期比较判断
			Calendar calendar = Calendar.getInstance();
			while (startDate.getTime()<=endDate.getTime()){
				// 把日期添加到集合
				list.add(sdf.format(startDate));
				// 设置日期
				calendar.setTime(startDate);
				//把日期增加一天
				calendar.add(Calendar.DATE, 1);
				// 获取增加后的日期
				startDate=calendar.getTime();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	public static List<String> getWeeks(String startTime, String endTime) {
		Date date = DateUtil.parse(startTime);
		Calendar c = Calendar.getInstance() ;
		c.setTime( date);
//获取开始时间是当年的第几周
		int startWeek = c.get(Calendar.WEEK_OF_YEAR);
//获取月份,如果月份是12,并且周数是1,重新赋值53周
		int startMonth = c.get(Calendar.MONTH) + 1;
		if (startMonth == 12 && startWeek ==1){
			startWeek = 53;
		}
//获取开始的年份
		int startYear = c.get(Calendar.YEAR);
		Date dateEnd = DateUtil.parse(endTime);
		Calendar cEnd = Calendar.getInstance() ;
		cEnd.setTime( dateEnd);
//获取结束时间是第几周
		int endWeek = cEnd.get(Calendar.WEEK_OF_YEAR);
//获取月份,如果月份是12,并且周数是1,重新赋值53周
		int endMonth = cEnd.get(Calendar.MONTH) + 1;
		if (endMonth ==12 && endWeek == 1){
			endWeek = 53;
		}
//获取结束时间的年份
		int endYear = cEnd.get(Calendar.YEAR);
		List<String> list = new ArrayList<>();
//默认每年53周
		if (endYear == startYear){
			for (int week = startWeek; week <= endWeek; week++){
				String yearAndWeek = startYear +"-" + week;
				list.add(yearAndWeek);
			}
		}
		if (endYear > startYear){
//计算相差年份
			int year = endYear - startYear;
//循环次数
			int count;
			if (year == 1){
//循环次数
				count = 53 - startWeek + endWeek;
			}else {
				int i = endYear - startYear;//循环次数
				count = 53 -startWeek + (i-1)*53 + endWeek;
			}
			for (int i = 0 ; i <= count; i++){

				String yearAndWeek = startYear + "-" + startWeek;
				if (startWeek < 10){
				yearAndWeek = startYear + "-0" + startWeek;
				}
				startWeek ++;
				if (startWeek == 54){
					startWeek = 1;
					startYear = startYear + 1;
				}
				list.add(yearAndWeek);
			}
		}
		return list;
	}

	public static List<String> getMonthBetweenDate(String startTime, String endTime){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		// 声明保存日期集合
		List<String> list = new ArrayList<String>();
		try {
			// 转化成日期类型
			Date startDate = sdf.parse(startTime);
			Date endDate = sdf.parse(endTime);

			//用Calendar 进行日期比较判断
			Calendar calendar = Calendar.getInstance();
			while (startDate.getTime()<=endDate.getTime()){
				// 把日期添加到集合
				list.add(sdf.format(startDate));
				// 设置日期
				calendar.setTime(startDate);
				//把日期增加一天
				calendar.add(Calendar.MONTH, 1);
				// 获取增加后的日期
				startDate=calendar.getTime();
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return list;
	}

}
 